import argparse
import logging.config
import os

from app.manager.manager import Manager
from app.fetch.fetch import Fetch
from app.checkout.checkout import Checkout
from app.manifest.manifest import Manifest
from app.metacheck.metacheck import MetaCheck
from app.mkcidocker.mkcidocker import Mkcidocker

_ = not os.path.exists("log") and os.mkdir("log")
logger_conf_path = "conf/logger.conf"
logging.config.fileConfig(logger_conf_path)

def init_args():
    """
    init args
    :return:
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("action")
    parser.add_argument("-api", type=str, dest="api_conf", default="api.yaml", required=False)
    parser.add_argument("-token", type=str, dest="databoard_token", default = "abc", required=False)
    parser.add_argument("-pr", type=int, dest="pr_num", default=0, required=False)
    parser.add_argument("-juser", type=str, dest="juser", default="", required=False)
    parser.add_argument("-jtoken", type=str, dest="jtoken", default="", required=False)
    parser.add_argument("-repo", type=str, dest="repo", default="", required=False)
    parser.add_argument("-sbranch", type=str, dest="sbranch", default="", required=False)
    parser.add_argument("-tbranch", type=str, dest="tbranch", default="", required=False)
    parser.add_argument("-owner", type=str, dest="owner", default="", required=False)
    parser.add_argument("-cmter", type=str, dest="commiter", default="", required=False)
    parser.add_argument("-gtoken", type=str, dest="gtoken", default="", required=False)
    parser.add_argument("-cid", type=str, dest="commentid", default="", required=False)
    parser.add_argument("-to_dir", type=str, dest="to_dir", default="", required=False)
    parser.add_argument("-src_dir", type=str, dest="src_dir", default="", required=False)
    parser.add_argument("-base_img", type=str, dest="base_img", default="", required=False)
    parser.add_argument("-target_img", type=str, dest="target_img", default="", required=False)

    return parser.parse_args()
    
def main():
    args = init_args()

    action = args.action
    if action == "manager":
        manager = Manager(
            api_conf = "api.yaml", 
            pr_num = args.pr_num, 
            repo_name = args.repo, 
            jenkins_base_url="https://openeulerjenkins.osinfra.cn/", 
            jenkins_user=args.juser, 
            jenkins_token=args.jtoken,
            gitee_owner=args.owner,
            gitee_token=args.gtoken)
        param = {
            "repo": args.repo, 
            "branch": args.sbranch, 
            "tbranch": args.tbranch, 
            "prid": args.pr_num, 
            "committer": args.commiter,
            "commentid": args.commentid}
        manager.exec(param)

    if action == "fetch":
        fetch = Fetch(
            to_dir = args.to_dir,
            repolist_dir = os.path.join(os.getcwd(), "conf/repolist.yaml"),
            repo_name = "yocto-meta-openeuler",
            gitee_owner = args.owner,
            gitee_token = args.gtoken)
        fetch.exec()

    if action == "checkout":
        checkout = Checkout(
            src_dir = args.src_dir
        )
        checkout.exec()
    
    if action == "manifest":
        manifest = Manifest()
        manifest.exec(args.src_dir)

    if action == "metacheck":
        metacheck = MetaCheck(
            meta_dir=os.path.join(os.getcwd(), "conf/metalist.yaml")
        )
        metacheck.exec(args.src_dir)

    if action == "mkcidocker":
        mkcidocker = Mkcidocker(
            base_image=args.base_img,
            conf_dir=os.path.abspath("conf")
        )
        mkcidocker.exec(args.target_img)

if __name__ == "__main__":
    main()